<script setup>
import { ref, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ElMessage } from "element-plus";
import axios from "axios";
import { useI18n } from "vue-i18n";
const I18n = useI18n();
const { locale } = useI18n();

const router = useRouter();
const route = useRoute();
const id = ref(route.query.id);

// 跳转页面
const jumpTo = (path) => {
  router.push({ path });
};

const quality = ref(0); // 质量
const attitude = ref(0); // 态度
const remark = ref(""); // 评价

const order = ref(null);
const getOrderDetail = async () => {
  try {
    const res = await axios.get(`${apiUrl}/order/detail/${id.value}`, {
      headers: {
        "Authori-zation": `Bearer ${getToken()}`,
      },
    });

    if (res.data.status === 200) {
      order.value = res.data.data;
    }
  } catch (e) {}
};

const commentOrder = async () => {
  if (quality.value === 0) {
    ElMessage({
      message: "请评价商品质量",
      type: "warning",
    });

    return;
  }
  if (attitude.value === 0) {
    ElMessage({
      message: "请评价服务质量",
      type: "warning",
    });
    return;
  }
  if (remark.value === "") {
    ElMessage({
      message: "请写下您的想法",
      type: "warning",
    });
    return;
  }
  try {
    const res = await axios.post(
      `${apiUrl}/order/comment`,
      {
        comment: remark.value,
        pics: [],
        product_score: quality.value,
        service_score: attitude.value,
        unique: order.value.cartInfo[0].unique,
      },
      {
        headers: {
          "Authori-zation": `Bearer ${getToken()}`,
        },
      }
    );

    if (res.data.status === 200) {
      ElMessage({
        message: "评价已提交",
        type: "success",
      });

      router.back();
    }
  } catch (e) {}
};

onMounted(() => {
  getOrderDetail();
});
</script>

<template>
  <div class="box">
    <div class="centerMiddle">
      <div class="top">
        <div class="orderList" v-if="order !== null">
          <div class="item">
            <div class="topOrderInfo">
              <div class="date">
                {{ $t("account.date") }}{{ order._add_time.split(" ")[0] }}
              </div>
              <div class="orderId">{{ $t("account.orderId") }}{{ id }}</div>
            </div>

            <div
              class="productInfo"
              v-for="(oitem, oindex) in order.cartInfo"
              :key="oindex"
            >
              <div class="leftBox">
                <div class="coverBox">
                  <img :src="oitem.productInfo.image" alt="" />
                </div>
                <div class="infoBox">
                  <div class="name">
                    {{
                      locale === "zh-cn"
                        ? oitem.productInfo.store_name
                        : oitem.productInfo.en_store_name
                    }}
                  </div>
                  <div class="specificationBox">
                    <div class="priceBox">
                      ￥{{ oitem.truePrice }}/{{ $t("account.unit") }}
                    </div>
                    <div class="num">{{ $t("account.num") }}{{ oitem.cart_num }}</div>
                  </div>
                  <div class="total">
                    {{ $t("account.gong") }}{{ oitem.cart_num }}{{ $t("account.count1")
                    }}<span class="totalPrice">￥{{ oitem.sum_price }}</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="rate">
          <div class="item">
            <div class="label">{{ $t("account.orderEvaluationContent4") }}</div>
            <div class="value">
              <el-rate v-model="quality" />
            </div>
          </div>
          <div class="item">
            <div class="label">{{ $t("account.orderEvaluationContent5") }}</div>
            <div class="value">
              <el-rate v-model="attitude" />
            </div>
          </div>

          <div class="item">
            <div class="value">
              <el-input
                v-model="remark"
                style="width: 850px"
                class="ttexrea"
                :rows="6"
                type="textarea"
                :placeholder="$t('account.orderEvaluationContent6')"
              />
            </div>
          </div>
        </div>

        <div class="opBox">
          <el-button
            type="primary"
            style="width: 150px; margin-top: 25px"
            @click="commentOrder"
            >{{ $t("account.orderEvaluationContent7") }}</el-button
          >
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.centerMiddle {
  width: 900px;
  margin: 30px auto;
  min-height: calc(100vh - 549px);
}

::v-deep .el-tabs__nav {
  margin-left: 150px !important;
}
::v-deep .el-tabs__content {
  overflow: unset;
}

.orderList {
  margin-top: 40px;
  // min-height: calc(100vh - 600px);
  .item {
    padding: 20px;
    padding-bottom: 0;
    background-color: #fff;
    margin-bottom: 30px;
    .topOrderInfo {
      display: flex;
      font-size: 14px;
      color: #666;

      .date {
        margin-right: 50px;
      }
    }

    .productInfo {
      display: flex;
      margin-top: 30px;
      justify-content: space-between;
      // border-bottom: 1px solid #f7f7f7;
      padding-bottom: 30px;

      .coverBox {
        width: 130px;
        height: 130px;
        background-color: #fbf7f7;
        border: 1px solid #f5f5f5;
        margin-right: 20px;

        img {
          width: 100%;
          height: 100%;
        }
      }

      .leftBox {
        display: flex;

        .name {
          font-size: 18px;
          font-weight: bold;
          margin-bottom: 15px;
          margin-top: 10px;
        }

        .specificationBox {
          display: flex;
          margin-bottom: 30px;
          position: relative;
          left: -2px;

          .priceBox {
            // font-weight: bold;
            color: #000;
          }

          .num {
            margin-left: 60px;
            color: #666;
          }
        }

        .total {
          color: #666;

          .totalPrice {
            font-size: 20px;
            color: #000;
          }

          .freight {
            color: #000;
          }
        }
      }
    }

    .opBox {
      padding: 5px 0;
      // margin-top: 20px;
      display: flex;
      justify-content: end;
      background-color: #fbf7f7;
    }
  }
}

.box {
  background-color: rgb(248, 248, 248);
  overflow: hidden;
}

.status {
  margin-top: 100px;
}

.rate {
  padding: 20px;
  background-color: #fff;
  .item {
    display: flex;
    margin-bottom: 20px;
    font-size: 14px;

    .label {
      line-height: 32px;
      width: 90px;
      font-size: 15px;
    }
  }
}

::v-deep .ttexrea .el-textarea__inner {
  box-shadow: none;
  background-color: rgb(248, 248, 248);
}
</style>
